package com.smforj.ssm.modules.api.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.frame.core.web.domain.DataResult.DataStatus;
import com.smforj.ssm.modules.lib.domain.CheckInResponse;
import com.smforj.ssm.modules.lib.domain.CheckOutResponse;
import com.smforj.ssm.modules.lib.domain.ItemResponse;
import com.smforj.ssm.modules.lib.domain.LibraryRequest;
import com.smforj.ssm.modules.lib.domain.PatronResponse;
import com.smforj.ssm.modules.lib.service.LibraryService;

/***
 *  图书馆自助借还系统Controller
 * 包含归还
 * 
 * @author Haijun Gao 
 * @date 2016-9-2 上午10:29:36
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */  
@RequestMapping("/oldlibrary/api")  
public class RLibraryController extends BaseControllerSupportImpl {
	
	private Logger log = Logger.getLogger(this.getClass());  
	
	@Autowired
	private LibraryService libraryService; 
	
	/***
	 * 查询读者信息
	 * @return LoginUser 包含SysReader
	 * @date 2016-8-11 下午2:03:45
	 */
	@RequestMapping(value={"/reader/{id}"},method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<PatronResponse> getReader(@PathVariable("id") String id)
	{ 
		System.out.println("LibraryController doCheckout");   
		try{
			LibraryRequest req = new LibraryRequest();
			req.setSzUserId(id);
			PatronResponse response = libraryService.getReader(req);
			return new DataResult<PatronResponse>((response.getStatus() == 1 ? DataStatus.OK: DataStatus.ERROR),"",response);
		}catch(Exception ex)
		{  
			log.error("获取用户权限失败: "+ex.getMessage()); 
			return new DataResult<PatronResponse>(DataStatus.ERROR, "获取用户权限失败: "+ex.getMessage());
		}   
	} 
	
	
	
	/***
	 * 归还 
	 * @return
	 * @date 2016-8-11 下午2:03:45
	 */
	@RequestMapping(value={"/book/{id}"},method = RequestMethod.POST, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult getBookInfo(@PathVariable("id") String id,@RequestParam(value="token",required=true) String token)
	{ 
		System.out.println("LibraryController doCheckout");
		log.info("bookid: "+id); 
		try{
			LibraryRequest req = new LibraryRequest();
			//req.setBookid(id);
			ItemResponse response = libraryService.getBookInfo(req);
			return new DataResult((response.getStatus() == 1 ? DataStatus.OK: DataStatus.ERROR),"",response); 
		}catch(Exception ex)
		{  
			log.error("获取用户权限失败: "+ex.getMessage()); 
			return new DataResult(DataStatus.ERROR, "获取用户权限失败: "+ex.getMessage());
		}   
	}  
	

	
 
	/***
	 * 从图书管理软件借阅图书 
	 * @return
	 * @date 2016-11-1 下午3:29:45
	 */
	@RequestMapping(value={"/borrow"},method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult doBorrow()
	{ 
		System.out.println("LibraryController doBorrow");  
		try{
			Object readerid = request.getSession().getAttribute("readerid"); 
			Object pwd = request.getSession().getAttribute("pwd"); 
			Object bookid = request.getSession().getAttribute("bookid"); 
			if(readerid == null || bookid == null)  return new DataResult(DataStatus.ERROR, "读者或图书信息不能为空"); 
			LibraryRequest req = new LibraryRequest();
			req.setSzUserId(readerid.toString());
			req.setSzUserPWD(pwd.toString());
			//req.setBookid(bookid.toString());
			CheckOutResponse response = libraryService.doCheckout(req).get(0);
			return new DataResult((response.getStatus() == 1 ? DataStatus.OK: DataStatus.ERROR),"",response); 
		}catch(Exception ex)
		{  
			log.error("借阅失败: "+ex.getMessage()); 
			return new DataResult(DataStatus.ERROR, "借阅失败: "+ex.getMessage());
		}   
	} 
	
	/***
	 * 从图书管理软件归还图书 
	 * @return
	 * @date 2016-11-1 下午3:29:45
	 */
	@RequestMapping(value={"/return/{id}"},method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult doReturn(@PathVariable("id") String id)
	{ 
		System.out.println("LibraryController doReturn");  
		try{ 
			LibraryRequest req = new LibraryRequest();
			req.setSzUserId(id); 
			//req.setBookid(id);
			CheckInResponse response = libraryService.doCheckin(req).get(0);
			return new DataResult((response.getStatus() == 1 ? DataStatus.OK: DataStatus.ERROR),"",response); 
		}catch(Exception ex)
		{  
			log.error("归还失败: "+ex.getMessage()); 
			return new DataResult(DataStatus.ERROR, "归还失败: "+ex.getMessage());
		}   
	} 
	
	/***
	 * 续借
	 * @return
	 * @date 2016-8-11 下午2:03:45
	 */
	@RequestMapping(value={"/renew"},method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult doRenew()
	{ 
		System.out.println("LibraryController doRenew");  
		try{
			Object readerid = request.getSession().getAttribute("readerid"); 
			Object pwd = request.getSession().getAttribute("pwd"); 
			Object bookid = request.getSession().getAttribute("bookid"); 
			if(readerid == null || bookid == null)  return new DataResult(DataStatus.ERROR, "读者或图书信息不能为空"); 
			LibraryRequest req = new LibraryRequest();
			req.setSzUserId(readerid.toString());
			req.setSzUserPWD(pwd.toString());
			//req.setBookid(bookid.toString());
			CheckOutResponse response = libraryService.doRenew(req).get(0);
			return new DataResult((response.getStatus() == 1 ? DataStatus.OK: DataStatus.ERROR),"",response); 
		}catch(Exception ex)
		{  
			log.error("续借失败: "+ex.getMessage()); 
			return new DataResult(DataStatus.ERROR, "续借失败: "+ex.getMessage());
		}  
	} 
	
	
}
